package com.tianqu.tms.www.rental.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.RentalContractInfo;
import com.tianqu.tms.entity.RentalLocationInfo;
import com.tianqu.tms.www.rental.actionform.RentalLocationAddInputForm;
import com.tianqu.tms.www.rental.viewhelper.RentalLocationViewHelper;;

public class RentalLocationAlterResultController extends TmsController {
	private Logger logger = Logger.getLogger(RentalLocationAlterResultController.class);
	private int userId = 0;

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TmsUserProfile file = uc.getUserProfile();
		this.userId = file.getUniqueId();
		TxController controller = TxController.getTxController();
		RentalLocationViewHelper view = (RentalLocationViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);

		// 添加事务提交异常捕获
		try {
			controller.begin();

			RentalLocationAddInputForm form = (RentalLocationAddInputForm) uc.getInputForm();
			view.setLocationSerial(form.getLocationSerial());
			view.setOldLocationSerial(uc.getParameter("oldLocationSerial"));
			view.setRentalType(form.getRentalType());
			view.setAcreage(form.getAcreage());
			view.setCarPort(form.getCarPort());
			view.setId(form.getId());
			view.setAddress(form.getAddress());
			view.setAnnualTransactionVolume(form.getAnnualTransactionVolume());
			
			RentalLocationInfo rentalLocationInfo = RentalLocationInfo.getInstance();
			RentalLocationInfo oldRentalLocationInfo = rentalLocationInfo.searchLocationDetailById(form.getId());
			
			// ID
			rentalLocationInfo.setId(form.getId());
			
			// 租位类型
			rentalLocationInfo.setRentalType(form.getRentalType());
			
			// 租位面积
			rentalLocationInfo.setAcreage(StringUtil.getint(form.getAcreage()));
			
			// 车位数量
			rentalLocationInfo.setCarPort(StringUtil.getint(form.getCarPort()));
			
			// 更新用户ID
			rentalLocationInfo.setUpdateUserId(userId);
			
			// 租位位置
			rentalLocationInfo.setAddress(form.getAddress());
			
			// 租位区域
			rentalLocationInfo.setArea(form.getRentalArea());
			
			//允许库存登记数量
			rentalLocationInfo.setAnnualTransactionVolume(form.getAnnualTransactionVolume());
			
			// 更新 市场租位信息表（tms_rentalLocation_info）
			rentalLocationInfo.updateLocationInfo(rentalLocationInfo);
			
			// 插入 租赁合同操作记录表（tms_contract_update_histroy）
			String remark = getRemark(oldRentalLocationInfo,rentalLocationInfo);
			rentalLocationInfo.setRemark(remark);
			rentalLocationInfo.setLocationSerial(form.getLocationSerial());
			rentalLocationInfo.setHandleType(2);//2：租位修改
			rentalLocationInfo.insertRentalUpdateHistroy(rentalLocationInfo);
			
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}
	
	
	private String getRemark (RentalLocationInfo oldRentalLocationInfo,RentalLocationInfo newRentalLocationInfo) throws TmsDaoException {
		StringBuffer remark = new StringBuffer();
		boolean flg = false;
		Codes codes = Codes.getInstance();
		
		if(null != oldRentalLocationInfo && null != newRentalLocationInfo) {
			// 租位类型
			if (oldRentalLocationInfo.getRentalType() != newRentalLocationInfo.getRentalType()) {
				if (flg) {
					remark.append("；");
				} else {
					flg = true;
				}
				
				remark.append("租位类型从").append(codes.getItemValueByItemIdAndItemCode(Codes.RENTAL_TYPE, oldRentalLocationInfo.getRentalType())).append("修改为").append(codes.getItemValueByItemIdAndItemCode(Codes.RENTAL_TYPE, newRentalLocationInfo.getRentalType()));
			}
			
			// 租位面积(㎡)
			if (oldRentalLocationInfo.getAcreage() != newRentalLocationInfo.getAcreage()) {
				if (flg) {
					remark.append("；");
				} else {
					flg = true;
				}
				
				remark.append("租位面积(㎡)从").append(oldRentalLocationInfo.getAcreage()).append("修改为").append(newRentalLocationInfo.getAcreage());
			}
			
			// 车位数量(个)
			if (oldRentalLocationInfo.getCarPort() !=  newRentalLocationInfo.getCarPort()) {
				if (flg) {
					remark.append("；");
				} else {
					flg = true;
				}
				
				remark.append("车位数量(个)从").append(oldRentalLocationInfo.getCarPort()).append("修改为").append(newRentalLocationInfo.getCarPort());
			}
			
			// 租位区域
			if (oldRentalLocationInfo.getArea() != newRentalLocationInfo.getArea()) {
				if (flg) {
					remark.append("；");
				} else {
					flg = true;
				}
				
				remark.append("租位区域从").append(codes.getItemValueByItemIdAndItemCode(Codes.RENTAL_AREA, oldRentalLocationInfo.getArea())).append("修改为").append(codes.getItemValueByItemIdAndItemCode(Codes.RENTAL_AREA, newRentalLocationInfo.getArea()));
			}
			
			// 租位位置
			if (!oldRentalLocationInfo.getAddress().equals(newRentalLocationInfo.getAddress())) {
				if (flg) {
					remark.append("；");
				} else {
					flg = true;
				}
				remark.append("租位位置从").append(oldRentalLocationInfo.getAddress()).append("修改为").append(newRentalLocationInfo.getAddress());
			}
		}
		
		return remark.toString();
	}
}

